import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/api/api.dart';
import 'package:fluwx/fluwx.dart';
import 'package:tobias/tobias.dart';

import 'toast_util.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/27 13:37
/// filePath： lib/util/pay_auth_share_util.dart
/// desc: 支付和身份验证工具
/// <summary>
///
class PayUtil{

  /// todo：支付宝支付
  /// [order] 订单
  static void alibabaPay(String order, {AliPayEvn evn = AliPayEvn.ONLINE,Function(int)? onCall}) async{
    var result = await isAliPayInstalled();
    if(result){ //去支付
      debugPrint("支付宝支付唤起订单：$order\n");
      await aliPay(order).then((payResult){
        print("payResult:$payResult");
        if (payResult['result'] != null) {
          if (payResult['resultStatus'] == "9000") {
            onCall!(0);
          }else if(payResult['resultStatus'] == "6001"){
            toast("支付失败，取消了支付！");
            onCall!(-1);
          }else{
            onCall!(-1);
          }
        }
      }).catchError((err){
        onCall!(-1);
      });
    }else{
      toast("当前未安装支付宝，无法继续支付！");
      onCall!(-1);
    }
  }

  /// todo：微信支付
  /// [order] 订单
  static void wxPay(WxModel wxModel,{Function? onCall}) async{
    if(await isWeChatInstalled){
      await payWithWeChat(
        appId: wxModel.appId!,
        partnerId: wxModel.partnerId!,
        prepayId: wxModel.prepayId!,
        packageValue: wxModel.packageValue!,
        nonceStr: wxModel.nonceStr!,
        timeStamp: wxModel.timeStamp!,
        sign: wxModel.sign!,
      ).then((value){
        if(onCall != null) onCall(value);
      });
    }else{
      toast("当前未安装微信，无法继续支付！");
    }
  }

  /// 分享文本
  static wxShareText({String? text,WeChatScene? scene}) async{
    if(await isWeChatInstalled){
      await shareToWeChat(WeChatShareTextModel("$text", scene: scene ??  WeChatScene.SESSION)).then((value){
        if(value) toast("分享成功");
      });
    }else{
      toast("当前未安装微信，分享失败！");
    }
  }

  /// 分享图像
  static wxShareImage({String? pic,String? title,String? description,String? webViewPath,WeChatScene? scene,Function? onCall}) async{
    if(await isWeChatInstalled){
      WeChatImage image = WeChatImage.network(pic ?? Api.testImg2);//显示的图片
      WeChatShareWebPageModel model = WeChatShareWebPageModel(
          webViewPath ?? "https://www.baidu.com",//webview 微信点击打开的页面
          title: title ?? '',//标题
          scene:scene ?? WeChatScene.SESSION,
          description: description ?? "",//描述
          thumbnail: image //显示的图片
      );
      await shareToWeChat(model).then((value){
        if(onCall!=null)onCall(value);
      });
    }else{
      toast("当前未安装微信，分享失败！");
    }
  }
}

/// 微信操作model
class WxModel{
  // 签名
  String? sign;
  // 预支付交易会话ID
  String? prepayId;
  // 商户号
  String? partnerId;
  // 应用ID
  String? appId;
  // 扩展字段
  String? packageValue;
  // 时间戳
  int? timeStamp;
  // 随机字符串
  String? nonceStr;

  WxModel(
      {this.sign,
      this.prepayId,
      this.partnerId,
      this.appId,
      this.packageValue,
      this.timeStamp,
      this.nonceStr});

  WxModel.toFormat(Map<String,dynamic> json){
    sign = json["sign"];
    prepayId = json["prepayId"];
    partnerId = json["partnerId"];
    appId = json["appId"];
    packageValue = json["packageValue"];
    timeStamp = int.parse(json["timeStamp"]);
    nonceStr = json["nonceStr"];
  }
}

